﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Sale_Model;
using Sale_Common;
using Sale_DAL;

namespace Sale_Operation
{
    public class BillListOp:Component
    {
        public override bool Save(object obj)
        {
            throw new NotImplementedException();
        }

        public override bool HasExists(object obj)
        {
            throw new NotImplementedException();
        }

        public override bool Delete(object obj)
        {
            throw new NotImplementedException();
        }

        public override DataTable GetList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            throw new NotImplementedException();
        }

        public override object GetModel(object obj)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// 获取销售单明细
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public DataTable GetSallBillList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id AS Id , SBill_Code AS Code, Cust_Name AS CustName ,SBill_User,SUser_Name,SBill_Type,SBill_Date,
                CASE SBill_State WHEN 1 THEN '结算' ELSE '未结算' END AS SBill_StateStr,SBill_State,B.SBDeta_Count,B.SBDeta_Price,B.SBDeta_Money,D.BData_Name FROM SaleBill A ");
                sbSql.Append(" LEFT JOIN SaleBillDetail B ON (A.SBill_Id=B.SBDeta_SBill_Id)");
                sbSql.Append(" LEFT JOIN SysUser C  ON (C.SUser_Id=A.SBill_User)");
                sbSql.Append(" LEFT JOIN BaseData D ON (D.BData_Id=A.SBill_Type)");
                sbSql.Append(" LEFT JOIN Customer E ON (E.Cust_Id = A.SBill_Cust_Id)");
                sbSql.Append(" WHERE A.State = 1 ");
                if (strCondition != "")
                    sbSql.Append(" AND " + strCondition);
                sbSql.Append(" ORDER BY A.SBill_Date DESC ");
                db.Data=SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount,LinkService.PageSize);
            }
            catch (Exception ex)
            { }
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取进货单明细
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public DataTable GetBuyBillList(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT BBill_Id AS Id , BBill_Code AS Code, Supp_Name AS CustName ,BBill_SUser_Id AS SBill_User,SUser_Name,'' SBill_Type,BBill_Date AS SBill_Date,CASE BBill_State WHEN 1 THEN '结算' ELSE '未结算' END AS BBill_StateStr,BBill_State AS SBill_State,BBDeta_Count AS SBDeta_Count,BBDeta_CostPrice AS SBDeta_Price,BBDeta_Money AS SBDeta_Money FROM BuyBill A ");
                sbSql.Append(" LEFT JOIN BuyBillDetail B ON (A.BBill_Id=B.BBDeta_SBill_Id)");
                sbSql.Append(" LEFT JOIN SysUser C  ON (C.SUser_Id=A.BBill_SUser_Id)");
                sbSql.Append(" LEFT JOIN Supplier D ON (D.Supp_Id=A.BBill_Supp_Id)");
                sbSql.Append(" WHERE A.State = 1 ");
                if (strCondition != "")
                    sbSql.Append(" AND " + strCondition);
                sbSql.Append(" ORDER BY A.BBill_Date DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
               
            }
            catch (Exception ex)
            { }
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取销售单
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public DataTable GetSaleBill(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id AS Id , SBill_Code AS Code, Cust_Name AS CustName ,SBill_User,SUser_Name,SBill_Type,SBill_Date,
                CASE SBill_State WHEN 1 THEN '结算' ELSE '未结算' END AS SBill_StateStr,SBill_State,'' SBDeta_Count,'' SBDeta_Price,SBill_RealMoney SBDeta_Money,D.BData_Name FROM SaleBill A ");
                
                sbSql.Append(" LEFT JOIN SysUser C  ON (C.SUser_Id=A.SBill_User)");
                sbSql.Append(" LEFT JOIN BaseData D ON (D.BData_Id=A.SBill_Type)");
                sbSql.Append(" LEFT JOIN Customer E ON (E.Cust_Id = A.SBill_Cust_Id)");
                sbSql.Append(" WHERE A.State = 1 ");
                if (strCondition != "")
                    sbSql.Append(" AND " + strCondition);
                sbSql.Append(" ORDER BY A.SBill_Date DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            { }
            return db.Data.Tables[0];
        }
        /// <summary>
        /// 获取销售单及其明细
        /// </summary>
        /// <param name="strCondition"></param>
        /// <param name="iCurrentPageIndex"></param>
        /// <param name="iRecordCount"></param>
        /// <returns></returns>
        public DataTable GetSaleBillDetail(string strCondition, int iCurrentPageIndex, ref int iRecordCount)
        {
            db = new DataBase();
            try
            {
                StringBuilder sbSql = new StringBuilder();
                sbSql.Append(@" SELECT SBill_Id AS Id , SBill_Code AS Code, Cust_Name AS CustName ,SBill_User,SUser_Name,SBill_Type,SBill_Date,
                CASE SBill_State WHEN 1 THEN '结算' ELSE '未结算' END AS SBill_StateStr,SBill_State,'' SBDeta_Count,'' SBDeta_Price,SBill_RealMoney SBDeta_Money,D.BData_Name,G.PInfo_Name,G.PInfo_Number,G.PInfo_CH,A.SBill_Code2 FROM SaleBill A ");

                sbSql.Append(" LEFT JOIN SysUser C  ON (C.SUser_Id=A.SBill_User)");
                sbSql.Append(" LEFT JOIN BaseData D ON (D.BData_Id=A.SBill_Type)");
                sbSql.Append(" LEFT JOIN Customer E ON (E.Cust_Id = A.SBill_Cust_Id)");
                sbSql.Append(" LEFT JOIN SaleBillDetail F ON (F.SBDeta_SBill_Id=A.SBill_Id AND F.State=1)");
                sbSql.Append(" LEFT JOIN ProductInfo G ON (G.PInfo_Id=F.SBDeta_PInfo_Id)");
                sbSql.Append(" WHERE A.State = 1 ");
                if (strCondition != "")
                    sbSql.Append(" AND " + strCondition);
                sbSql.Append(" ORDER BY A.SBill_Date DESC ");
                db.Data = SqlHelper.FillDataSet(db.Connection, sbSql.ToString(), iCurrentPageIndex, ref iRecordCount, LinkService.PageSize);
            }
            catch (Exception ex)
            {
                LinkService.InsertLog(ESysLogType.Error, "退货管理中选择销售单的时候报错！["+ex.ToString()+"]！", EMenuList.退货管理);
            }
            return db.Data.Tables[0];
        }
    }

}
